import { ForbiddenError } from '@nestjs/apollo';
import { Args, Mutation, Query, Resolver } from '@nestjs/graphql';
import { MediaEntity } from 'src/media/media.entity';
import { MediaService } from 'src/media/media.service';
import { PrismaService } from 'src/prisma/prisma.service';
import { WorkEntity } from './work.entity';
import * as moment from 'moment';
import { StudentViewEntity } from './student.view.entity';
@Resolver()
export class WorkResolver {
    constructor(
        private readonly prismaService: PrismaService,
        private readonly mediaService: MediaService
    ) { }

    @Query(() => [WorkEntity])
    getWorks() {
        return this.prismaService.work.findMany({
            orderBy: {
                id: "desc"
            }
        })
    }

    @Mutation(() => WorkEntity)
    async addTask(
        @Args("deadline") deadline: string,
        @Args("name") name: string
    ) {
        const work = await this.prismaService.work.findUnique({
            where: {
                name
            }
        })
        if (!!work) {
            throw new ForbiddenError("添加失败 作业已存在")
        }
        return await this.prismaService.work.create({
            data: {
                name,
                deadline: moment(deadline).toDate()
            }
        })
    }

    @Mutation(() => WorkEntity)
    updTask(
        @Args("deadline") deadline: string,
        @Args("name") name: string,
        @Args("id") id: number
    ) {
        return this.prismaService.work.update({
            where: {
                id
            },
            data: {
                name,
                deadline: moment(deadline).toDate()
            }
        })
    }

    @Mutation(() => MediaEntity)
    async getMediaId(
        @Args("name") name: string,
        @Args("num_id") num_id: string,
        @Args("work_id") work_id: number
    ) {
        const student = await this.prismaService.student.findFirst({
            where: {
                name,
                num_id
            }
        })
        if (!student) throw new ForbiddenError("用户不存在")
        const work = await this.prismaService.work.findFirst({
            where: {
                id: work_id
            }
        })
        if (!work) throw new ForbiddenError("作业不存在")
        return await this.mediaService.createMedia(work, student);
    }


    @Mutation(() => [StudentViewEntity])
    async workView(
        @Args("id") id: number
    ) {
        const work = await this.prismaService.work.findFirst({
            where: {
                id
            },
            include: {
                media: {
                    include: {
                        student: true
                    }
                }
            }
        });
        if (!work) throw new ForbiddenError("找不到作业")
        const students = await this.prismaService.student.findMany({
            include: {
                profile: true
            }
        });
        return students.map((item: StudentViewEntity) => {
            const media = work.media.find(e => e.studentId === item.id)
            item.complish = !!media
            item.mediaId = media?.id
            return item;
        })
    }

}
